package adapteurMediaStore;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;

import com.insta.playdroid.AlbumActivity2;
import com.insta.playdroid.ArtistActivity2;
import com.insta.playdroid.metier.domain.Album;
import com.insta.playdroid.metier.domain.Artist;
import com.insta.playdroid.metier.domain.Song;

public class SongUtils implements ISongUtils {

	private Album albumSelect;
	private Artist artistSelect;
	private Context context;

	public SongUtils(Context context) {
		super();
		this.context = context;
	}

	/**
	 * select list des songs de l'album
	 * 
	 * @param position
	 */
	@Override
	public List<Song> getSongAlbum(int position) {
		int id;
		String titre;
		String path;
		String artist;
		List<Song> songs = new ArrayList<Song>();

		albumSelect = AlbumActivity2.albums.get(position);

		// the cursor we will use to iterate over the db results
		Cursor cursor = null;

		// the list of columns our search relates to
		final String[] projection = new String[] {
				MediaStore.Audio.AudioColumns._ID,
				MediaStore.Audio.AudioColumns.TITLE,
				MediaStore.Audio.AudioColumns.DATA,
				MediaStore.Audio.AudioColumns.ARTIST };

		// how we want to sort our results
		final String sortOrder = AudioColumns.TITLE + " COLLATE LOCALIZED ASC";

		// the uri of the table that we want to query
		Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

		// launch the query
		cursor = context.getContentResolver()
				.query(uri, projection, AudioColumns.ALBUM_ID + "=?",
						new String[] { String.valueOf(albumSelect.getId()) },
						sortOrder);

		if (cursor != null) {
			songs = new ArrayList<Song>(cursor.getCount());

			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				id = cursor.getInt(0);
				titre = cursor.getString(1);
				path = cursor.getString(2);
				artist = cursor.getString(3);

				songs.add(new Song(id, titre, path, artist, albumSelect
						.getTitre(), albumSelect.getAlbumArt()));

				cursor.moveToNext();

			}

		}
		return songs;
	}

	/**
	 * select list des songs de l'artist
	 * 
	 * @param position
	 */
	@Override
	public List<Song> getSongArtist(int position) {
		int id;
		String titre;
		String path;
		String artist;
		String album;

		List<Song> songs = new ArrayList<Song>();
		artistSelect = ArtistActivity2.artists.get(position);

		// the cursor we will use to iterate over the db results
		Cursor cursor = null;

		// the list of columns our search relates to
		final String[] projection = new String[] {
				MediaStore.Audio.AudioColumns._ID,
				MediaStore.Audio.AudioColumns.TITLE,
				MediaStore.Audio.AudioColumns.DATA,
				MediaStore.Audio.AudioColumns.ARTIST,
				MediaStore.Audio.AudioColumns.ALBUM };

		// how we want to sort our results
		final String sortOrder = AudioColumns.TITLE + " COLLATE LOCALIZED ASC";

		// the uri of the table that we want to query
		Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

		// launch the query
		cursor = context.getContentResolver().query(uri, projection,
				AudioColumns.ARTIST_ID + "=?",
				new String[] { String.valueOf(artistSelect.getId()) },
				sortOrder);

		if (cursor != null) {
			songs = new ArrayList<Song>(cursor.getCount());

			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				id = cursor.getInt(0);
				titre = cursor.getString(1);
				path = cursor.getString(2);
				artist = cursor.getString(3);
				album = cursor.getString(4);

				songs.add(new Song(id, titre, path, artist, album, null));
				cursor.moveToNext();

			}

		}
		return songs;
	}

	@Override
	public void deleteTrack(Context context, String localPath) {
		Uri uri = MediaStore.Audio.Media.getContentUriForPath(localPath);
		context.getContentResolver().delete(uri, null, null);
	}
}
